﻿// -----------------------------------------------------------------------
// <copyright file="DmBenhNhanDataAccess.cs" company="">
// TODO: Copyright by dangppsoft@gmail.com.
// </copyright>
// -----------------------------------------------------------------------
namespace ManagerClinic.Core.DataAccess
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ManagerClinic.Core.Common;
    using System.Data.Common;
    using System.Data;
    using Microsoft.Practices.EnterpriseLibrary.Data;
    using Microsoft.Practices.EnterpriseLibrary.Common;
    using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
    using ManagerClinic.Ultilities.Ultility;

    public class DmBenhNhanDataAccess
    {
        public bool InsertDmBenhNhan(DmBenhNhanEntity obj)
        {
            try
            {
                string sql = "spDmBenhNhan_Insert";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaBenhNhan", DbType.String, obj.MaBenhNhan);
                db.AddInParameter(cmd, "TenBenhNhan", DbType.String, obj.TenBenhNhan);
                db.AddInParameter(cmd, "NgaySinh", DbType.String, obj.NgaySinh);
                db.AddInParameter(cmd, "ThangSinh", DbType.String, obj.ThangSinh);
                db.AddInParameter(cmd, "NamSinh", DbType.Int32, obj.NamSinh);
                db.AddInParameter(cmd, "MaGioiTinh", DbType.String, obj.MaGioiTinh);
                db.AddInParameter(cmd, "MaDanToc", DbType.String, obj.MaDanToc);
                db.AddInParameter(cmd, "MaNgheNghiep", DbType.String, obj.MaNgheNghiep);
                db.AddInParameter(cmd, "MaQuocGia", DbType.String, obj.MaQuocGia);
                db.AddInParameter(cmd, "MaTinhThanh", DbType.String, obj.MaTinhThanh);
                db.AddInParameter(cmd, "MaQuanHuyen", DbType.String, obj.MaQuanHuyen);
                db.AddInParameter(cmd, "ThonPho", DbType.String, obj.ThonPho);
                db.AddInParameter(cmd, "NoiLamViec", DbType.String, obj.NoiLamViec);
                db.AddInParameter(cmd, "SoDienThoai", DbType.String, obj.SoDienThoai);
                db.AddInParameter(cmd, "Email", DbType.String, obj.Email);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("InsertDmBenhNhan", ex.Message); throw ex; }
        }

        public bool EditDmBenhNhan(DmBenhNhanEntity obj)
        {
            try
            {
                string sql = "spDmBenhNhan_Update";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaBenhNhan", DbType.String, obj.MaBenhNhan);
                db.AddInParameter(cmd, "TenBenhNhan", DbType.String, obj.TenBenhNhan);
                db.AddInParameter(cmd, "NgaySinh", DbType.String, obj.NgaySinh);
                db.AddInParameter(cmd, "ThangSinh", DbType.String, obj.ThangSinh);
                db.AddInParameter(cmd, "NamSinh", DbType.Int32, obj.NamSinh);
                db.AddInParameter(cmd, "MaGioiTinh", DbType.String, obj.MaGioiTinh);
                db.AddInParameter(cmd, "MaDanToc", DbType.String, obj.MaDanToc);
                db.AddInParameter(cmd, "MaNgheNghiep", DbType.String, obj.MaNgheNghiep);
                db.AddInParameter(cmd, "MaQuocGia", DbType.String, obj.MaQuocGia);
                db.AddInParameter(cmd, "MaTinhThanh", DbType.String, obj.MaTinhThanh);
                db.AddInParameter(cmd, "MaQuanHuyen", DbType.String, obj.MaQuanHuyen);
                db.AddInParameter(cmd, "ThonPho", DbType.String, obj.ThonPho);
                db.AddInParameter(cmd, "NoiLamViec", DbType.String, obj.NoiLamViec);
                db.AddInParameter(cmd, "SoDienThoai", DbType.String, obj.SoDienThoai);
                db.AddInParameter(cmd, "Email", DbType.String, obj.Email);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("UpdateDmBenhNhan", ex.Message); throw ex; }
        }

        public bool DeleteDmBenhNhan(string maBenhNhan)
        {
            try
            {
                string sql = "spDmBenhNhan_DeleteByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaBenhNhan", DbType.String, maBenhNhan);
                int result = (int)db.ExecuteNonQuery(cmd);
                return (result > 0) ? true : false;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("DeleteDmBenhNhan", ex.Message); throw ex; }
        }

        public DmBenhNhanEntity GetDmBenhNhan(string maBenhNhan)
        {
            try
            {
                DmBenhNhanEntity obj = null;
                string sql = "spDmBenhNhan_SelectByID";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                db.AddInParameter(cmd, "MaBenhNhan", DbType.String, maBenhNhan);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DmBenhNhanEntity();
                        obj.MaBenhNhan = reader["MaBenhNhan"].ToString();
                        obj.TenBenhNhan = reader["TenBenhNhan"].ToString();
                        obj.NgaySinh = reader["NgaySinh"].ToString();
                        obj.ThangSinh = reader["ThangSinh"].ToString();
                        obj.NamSinh = String.IsNullOrEmpty(reader["NamSinh"].ToString()) == true ? 0 : (Int32)reader["NamSinh"];
                        obj.MaGioiTinh = reader["MaGioiTinh"].ToString();
                        obj.MaDanToc = reader["MaDanToc"].ToString();
                        obj.MaNgheNghiep = reader["MaNgheNghiep"].ToString();
                        obj.MaQuocGia = reader["MaQuocGia"].ToString();
                        obj.MaTinhThanh = reader["MaTinhThanh"].ToString();
                        obj.MaQuanHuyen = reader["MaQuanHuyen"].ToString();
                        obj.ThonPho = reader["ThonPho"].ToString();
                        obj.NoiLamViec = reader["NoiLamViec"].ToString();
                        obj.SoDienThoai = reader["SoDienThoai"].ToString();
                        obj.Email = reader["Email"].ToString();
                        obj.TenGioiTinh = reader["TenGioiTinh"].ToString();
                        obj.TenDanToc = reader["TenDanToc"].ToString();
                        obj.TenQuocGia = reader["TenQuocGia"].ToString();
                        obj.TenNgheNghiep = reader["TenNgheNghiep"].ToString();
                        obj.TenTinhThanh = reader["TenTinhThanh"].ToString();
                        obj.TenQuanHuyen = reader["TenQuanHuyen"].ToString();
                    }
                }
                return obj;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetDmBenhNhan", ex.Message); throw ex; }
        }

        public List<DmBenhNhanEntity> GetDmBenhNhanList()
        {
            try
            {
                List<DmBenhNhanEntity> list = new List<DmBenhNhanEntity>();
                DmBenhNhanEntity obj = null;
                string sql = "spDmBenhNhan_SelectAll";
                Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>();
                DbCommand cmd = db.GetStoredProcCommand(sql);
                using (IDataReader reader = db.ExecuteReader(cmd))
                {
                    while (reader.Read())
                    {
                        obj = new DmBenhNhanEntity();
                        obj.MaBenhNhan = reader["MaBenhNhan"].ToString();
                        obj.TenBenhNhan = reader["TenBenhNhan"].ToString();
                        obj.NgaySinh = reader["NgaySinh"].ToString();
                        obj.ThangSinh = reader["ThangSinh"].ToString();
                        obj.NamSinh = String.IsNullOrEmpty(reader["NamSinh"].ToString()) == true ? 0 : (Int32)reader["NamSinh"];
                        obj.MaGioiTinh = reader["MaGioiTinh"].ToString();
                        obj.MaDanToc = reader["MaDanToc"].ToString();
                        obj.MaNgheNghiep = reader["MaNgheNghiep"].ToString();
                        obj.MaQuocGia = reader["MaQuocGia"].ToString();
                        obj.MaTinhThanh = reader["MaTinhThanh"].ToString();
                        obj.MaQuanHuyen = reader["MaQuanHuyen"].ToString();
                        obj.ThonPho = reader["ThonPho"].ToString();
                        obj.NoiLamViec = reader["NoiLamViec"].ToString();
                        obj.SoDienThoai = reader["SoDienThoai"].ToString();
                        obj.Email = reader["Email"].ToString();
                        obj.TenGioiTinh = reader["TenGioiTinh"].ToString();
                        obj.TenDanToc = reader["TenDanToc"].ToString();
                        obj.TenQuocGia = reader["TenQuocGia"].ToString();
                        obj.TenNgheNghiep = reader["TenNgheNghiep"].ToString();
                        obj.TenTinhThanh = reader["TenTinhThanh"].ToString();
                        obj.TenQuanHuyen = reader["TenQuanHuyen"].ToString();
                        list.Add(obj);
                    }
                }
                return list;
            }
            catch (Exception ex) { LogExceptionUltilities.LogException("GetDmBenhNhanList", ex.Message); throw ex; }
        }

    }
}